package com.stylefeng.guns.rest.modular.wz.api.v3;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.rest.common.base.controller.ApiBaseController;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.enums.EnWzAdvMap;
import com.stylefeng.guns.rest.enums.EnWzMapActivity;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.api.v3.dao.WzAdvMapDao;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.IWzAdvMapService;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.IWzMapActivityService;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.impl.MapActivityServiceImpl;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.impl.WzAdvMapServiceImpl;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.service.IPfFileService;
import com.stylefeng.guns.rest.modular.wz.service.IWzUserInfoService;
import com.stylefeng.guns.rest.modular.wz.service.IWzUserService;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzAdvServiceImpl;
import com.stylefeng.guns.rest.modular.wz.util.AliOssUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 享低价活动
 */
@RestController
@RequestMapping("/wz/api/v3/mapActivity/")
public class WzMapActivityApi extends ApiBaseController {
    @Autowired
    private MapActivityServiceImpl mapActivityService;
    @Autowired
    private CacheService cacheService;

    /**
     * 新增活动
     */
    @PostMapping("add")
    public Res add(@RequestBody ParamMap paramMap) throws Exception{
        Res res=mapActivityService.addMapActivity(paramMap);
        return res;
    }


    /**
     * 活动列表
     */
    @PostMapping("list")
    public Res list(@RequestBody ParamMap paramMap){
        //位置判断
        String switchAdCode = paramMap.getStrValid("switchAdCode");
        if(StringUtils.isBlank(switchAdCode)){
            switchAdCode= paramMap.getStrMisValid("adCode");
        }
        if(switchAdCode.length() != 6){
            return Res.fail(BizExceptionEnum.RES_Ad_Code_Abnormal);
        }
        Wrapper e = new EntityWrapper<WzMapActivity>().like("ad_code",switchAdCode).eq("status", EnWzMapActivity.STATUS_RELEASE.getIndex()).ge("end_time", new Date()).gt("surplus_number",0);
        //主页扫码活动
        Long advMapId = paramMap.getLongValid("advMapId");//商户店铺id
        if(advMapId != null){
            e.eq("adv_map_id", advMapId);
        }
        //全部活动
        String keyWords = paramMap.getStrValid("keyWords");//搜索关键字
        Integer advMapCategoryFirst = paramMap.getIntValid("advMapCategoryFirst");//0:全部
        if(advMapCategoryFirst != 0){
            e.eq("adv_map_category_first", advMapCategoryFirst);
        }
        if(StringUtils.isNotBlank(keyWords)){
            String filterSql = "(name like '%" + keyWords+"%' or benefit like '%"+keyWords+"%')";
            e.addFilter(filterSql);
        }
        Res res=mapActivityService.list(paramMap,e);
        return res;
    }

    /**
     * 我参加的活动列表
     */
    @PostMapping("myList")
    public Res myList(@RequestBody ParamMap paramMap){
        Res res=mapActivityService.myList(paramMap);
        return res;
    }

    /**
     * 活动详情
     */
    @PostMapping("detail")
    public Res detail(@RequestBody ParamMap paramMap){
        Res res=mapActivityService.detail(paramMap);
        return res;
    }

    /**
     * 扫码参与活动
     */
    @PostMapping("scanJoin")
    public Res scanJoin(@RequestBody ParamMap paramMap) throws Exception {
        Res res=mapActivityService.scanJoinActivity(paramMap);
        return res;
    }

    /**
     * 删除地图活动
     */
    @PostMapping("delete")
    public Res deleteActivity(@RequestBody ParamMap paramMap) throws Exception {
        Res res=mapActivityService.deleteActivity(paramMap);
        return res;
    }

    /**
     * 修改地图活动
     */
    @PostMapping("edit")
    public Res editActivity(@RequestBody ParamMap paramMap) throws Exception{
        Res res=mapActivityService.edit(paramMap);
        return res;
    }

    /**
     * 参与活动
     */
    @PostMapping("join")
    public Res join(@RequestBody ParamMap paramMap) throws Exception {
        Long mapActivityId = paramMap.getLongMisValid("mapActivityId");
        String adCode = paramMap.getStrMisValid("adCode");
        //位置判断
        if(adCode.length() != 6){
            return Res.fail(BizExceptionEnum.RES_Ad_Code_Abnormal);
        }
        WzMapActivity mapActivity = cacheService.mapActivity(mapActivityId);
        List<String> acAdCode= Arrays.asList(mapActivity.getAdCode().split(","));
        boolean f=acAdCode.contains(adCode);
        if(!f){
            return Res.fail("当前区域暂时不可参加！");
        }
        Res res=mapActivityService.joinActivity(paramMap);
        return res;
    }

}
